//
//  RxDataSourceSectionAutoTableVC.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/5/24.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift
import RxDataSources
import CooperSwift

struct CustomSection {
    var header: String
    var items: [Item]
}

extension CustomSection : AnimatableSectionModelType {
    typealias Item = String
    
    init(original: CustomSection, items: [String]) {
        self = original
        self.items = items
    }
    
    var identity: String {
        return header
    }
    
}

class RxDataSourceSectionAutoTableVC: UIViewController, TableEnableProtocol {
    
    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let tv = tableView
        
        //初始化数据
        let sections = Observable.just([
            CustomSection(header: "", items: [
                "UILable的用法",
                "UIText的用法",
                "UIButton的用法"
                ])
            ])
         
        //创建数据源
        let dataSource = RxTableViewSectionedAnimatedDataSource<CustomSection>(
            //设置单元格
            configureCell: { ds, tv, ip, item in
                let cell = tv.dequeueReusableCell(withIdentifier: "Cell")
                    ?? UITableViewCell(style: .default, reuseIdentifier: "Cell")
                cell.textLabel?.text = "\(ip.row)：\(item)"
                return cell
            },
            canMoveRowAtIndexPath: { dataSource, index in
                return false
            }
        )
        //绑定单元格数据
        sections
            .bind(to: tv.rx.items(dataSource: dataSource))
            .disposed(by: disposeBag)
        // Do any additional setup after loading the view.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
